package org.example.zhihuan.util;

import java.sql.*;

/**
 * create by 2022/12/12.
 *
 * @author udbjqr
 */

public class Db {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private static String url = "jdbc:mysql://192.168.0.106:3306/zhihuan";
    private static String userName = "root";
    private static String passwd = "82094511";

    public static <T>T read(String sql, RunResultSet<T> handle) {
        System.out.println(sql);
        try (

                Connection conn = DriverManager.getConnection(url, userName, passwd);
                Statement statement = conn.createStatement();
                ResultSet set = statement.executeQuery(sql);
        ) {
          return handle.handle(set);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean execute(String sql) {
        System.out.println(sql);
        try (
                Connection conn = DriverManager.getConnection(url, userName, passwd);
                Statement statement = conn.createStatement();
        ) {
            return statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
